package com.tsing.mapper.impl;

import com.tsing.common.framwork.mapper.CommonDao;
import com.tsing.mapper.PostDetailsMapper;
import com.tsing.pojo.entity.TlDetails;
import com.tsing.pojo.entity.TlPost;
import com.tsing.pojo.entity.TlUser;

import java.sql.SQLException;
import java.util.List;

/**
 * @auth 卢孝宇
 * @date 2023/12/12 10:16
 */
public class PostDetailsMapperImpl extends CommonDao implements PostDetailsMapper {
    @Override
    public List<TlDetails> list(Integer postId) {
        List<TlDetails> list = null;
        Object[] params = {postId};
        try {
            list = executeQuery(TlDetails.class, "select * from tl_details where postId = ? order by recoveryTime desc",
                    params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public Integer likeCount(Long detailsId) {
        Integer list = null;
        Object[] params = {detailsId};
        try {
            list = executeSingleQuery(Integer.class, "select count(*) from tl_love where detailsId = ?",
                    params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public TlUser userById(Long userId) {
        TlUser list = null;
        Object[] params = {userId};
        try {
            list = executeSingleQuery(TlUser.class, "select * from tl_user where userId = ?",
                    params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public Boolean flag(Long detailsId, Integer userId) {
        Integer list = null;
        Object[] params = {userId, detailsId};
        try {
            list = executeSingleQuery(Integer.class, "select count(*) from tl_love where userId = ? and detailsId = ?",
                    params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list > 0;
    }

    @Override
    public Integer add(Integer detailsId, Integer userId) {
        Integer list = null;
        Object[] params = {userId, detailsId};
        try {
            list = executeUpdate("insert into tl_love(userId,detailsId) values(?,?)",
                    params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public Integer delete(Integer detailsId, Integer userId) {
        Integer list = null;
        Object[] params = {userId, detailsId};
        try {
            list = executeUpdate("delete from tl_love where userId = ? and detailsId = ?",
                    params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public Integer likeCountAdd(Integer detailsId) {
        Integer list = null;
        Object[] params = {detailsId};
        try {
            list = executeUpdate("update tl_details set likeCount = likeCount + 1 where detailsId" +
                            " = ?",
                    params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public Integer likeCountDelete(Integer detailsId) {
        Integer list = null;
        Object[] params = {detailsId};
        try {
            list = executeUpdate("update tl_details set likeCount = likeCount - 1 where detailsId" +
                            " = ?",
                    params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public TlPost postInfo(Integer postId) {
        TlPost list = null;
        Object[] params = {postId};
        try {
            list = executeSingleQuery(TlPost.class, "select * from tl_post where postId = ?", params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    @Override
    public Boolean deleteFollow(Integer followId, Integer userId) {
        Integer list = null;
        Object[] params = {followId,userId};
        try {
            list = executeUpdate( "delete from tl_interestid where followId = ? and userId = ?", params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list>0;
    }

    @Override
    public Boolean addFollow(Integer followId, Integer userId) {
        Integer list = null;
        Object[] params = {followId,userId};
        try {
            list = executeUpdate( "insert into tl_interestid(followId,userId) values(?,?)", params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return list>0;
    }
}
